﻿using System.Collections.Generic;

namespace Kugar.Core.Security
{
    public abstract class PermissionProviderBase : IPermissionProvider
    {
        public abstract bool CheckUser(string userName, string password);

        public virtual IUserPermissionChecker GetUserPermissionProvider(int userID)
        {
            return new UserPermissionManager(userID,PermissionManager);
        }

        public IUserGroupManager GroupManager { get; protected set; }
        public IUserSpecialManager UserSpecialPermissionManager { get; protected set; }
        public IPermissionCheckProvider PermissionManager { get; protected set; }

        private class UserPermissionManager : IUserPermissionChecker
        {
            //private MSSQL_UserGroupManager _groupManager = null;
            //private MSSQL_UserSpecialManager _userSpecialManager = null;
            //private Func<SQLDataHelper> _connGetter = null;
            //private bool _isAutoDisposeConn = false;
            private IPermissionCheckProvider _permissionProvider = null;

            private int _userID = -1;

            public UserPermissionManager(int userID,IPermissionCheckProvider permissionProvider  )
            {
                _userID = userID;
                _permissionProvider = permissionProvider;
            }

            public int CheckPermissionByID(int permissionID)
            {
                return _permissionProvider.CheckPermissionByID(_userID, permissionID);
            }

            public int CheckPermissionByName(string permissionName)
            {
                return _permissionProvider.CheckPermissionByName(_userID, permissionName);
            }

            public KeyValuePair<int, int>[] CheckPermissionByIDList(int[] permissionIDList)
            {
                return _permissionProvider.CheckPermissionByIDList(_userID, permissionIDList);
            }

            public KeyValuePair<string, int>[] CheckPermissionByNameList(string[] permissionNameList)
            {
                return _permissionProvider.CheckPermissionByNameList(_userID, permissionNameList);
            }

            public bool HasPermission(string permissionName)
            {
                return _permissionProvider.HasPermission(_userID, permissionName);
            }

            public bool HasPermission(string[] permissionNameList)
            {
                return _permissionProvider.HasPermission(_userID, permissionNameList);
            }

            public bool HasPermission(int permissionID)
            {
                return _permissionProvider.HasPermission(_userID, permissionID);
            }

            public bool HasPermission(int[] permissionIDList)
            {
                return _permissionProvider.HasPermission(_userID, permissionIDList);
            }

            public RMS_PermissionValueCollection GetPermissionListByUserID()
            {
                return _permissionProvider.GetPermissionListByUserID(_userID);
            }
        }
    }
}